package org.jeecg.modules.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.message.constant.MessageConstant;
import org.jeecg.modules.message.entity.MessageEmailConfig;
import org.jeecg.modules.message.mapper.MessageEmailConfigMapper;
import org.jeecg.modules.message.service.IMessageEmailConfigService;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 邮件配置
 * @Author: sean
 * @Date:   2024-10-09
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class MessageEmailConfigServiceImpl extends ServiceImpl<MessageEmailConfigMapper, MessageEmailConfig> implements IMessageEmailConfigService {

    private final CacheManager cacheManager;

    @Override
    public IPage<MessageEmailConfig> selelctJoinPage(Page page, Wrapper wrapper) {
        return baseMapper.selelctJoinPage(page, wrapper);
    }

    @Override
    public MessageEmailConfig get(String tenantId) {
        Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
        String key = TenantContext.getTenant();
        MessageEmailConfig entity = cache.get(key, MessageEmailConfig.class);
        if(oConvertUtils.isEmpty(entity)){
            entity = baseMapper.getOneNoTenant(new QueryWrapper<>());
            if(oConvertUtils.isNotEmpty(entity)){
                cache.put(key, entity);
            }
        }
        return entity;
    }

//    @Override
//    public MessageEmailConfig get() {
//        Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
//        String key = TenantContext.getTenant();
//        MessageEmailConfig entity = cache.get(key, MessageEmailConfig.class);
//        if(oConvertUtils.isEmpty(entity)){
//            entity = this.getOne(new QueryWrapper<>());
//            if(oConvertUtils.isNotEmpty(entity)){
//                cache.put(key, entity);
//            }
//        }
//        return entity;
//    }

    @Override
    public MessageEmailConfig add(MessageEmailConfig entity) {
        if(this.saveOrUpdate(entity)){
            Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
            String key = TenantContext.getTenant();
            cache.put(key, entity);
        }
        return entity;
    }

    @Override
    public MessageEmailConfig edit(MessageEmailConfig entity) {
        if(this.saveOrUpdate(entity)){
            Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
            String key = TenantContext.getTenant();
            cache.put(key, entity);
        }
        return entity;
    }

    @Override
    public Boolean deleteById(String id) {
        String keyToDelete = TenantContext.getTenant();
        Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
        cache.evict(keyToDelete);
        return this.removeById(id);
    }

    @Override
    public Boolean deleteBatch(List<String> ids) {
        Cache cache = cacheManager.getCache(MessageConstant.MESSAGE_EMAIL_CONFIG_CACHE);
        if (cache != null) {
            ids.forEach(item->{
                String keyToDelete = TenantContext.getTenant();
                cache.evict(keyToDelete);
            });
        }
        return this.removeByIds(ids);
    }


}
